<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>回调页面</title>
    <script src="plugins/vue/dist/vue.js"></script>
    <script src="plugins/axios/dist/axios.js"></script>
    <script src="script/common.js"></script>
</head>
<body>
    <div id="myDiv"></div>

    <script type="text/javascript">
        new Vue({
            el:"#myDiv",
            data:{

            },
            mounted(){
                // 通过code调用后端微信登录接口
                var obj = parseUrl();
                this.$http.post("/login/wechat/"+obj.code)
                    .then(result => {
                        result = result.data;
                        if(result.success){
                            var res = result.resultObj;
                            if(res.msg == "noBind"){
                                // 如果后端返回的msg==noBind,当前微信用户还没有绑定手机号,要跳转到绑定页面
                                var access_token = res.access_token;
                                var openid = res.openid;
                                location.href = "/binder.html?accessToken="+access_token+"&openid="+openid;
                            }else{
                                // 说明当前登录成功
                                // 将token和loginUser存入localStorage中
                                localStorage.setItem("token",result.resultObj.token);
                                localStorage.setItem("loginUser",JSON.stringify(result.resultObj.loginUser));
                                // 跳转到index.html
                                location.href = "/index.html";
                            }
                        }else{
                            alert(result.message);
                        }
                    })
                    .catch(result => {
                        alert("登录失败");
                    })
            }
        })
    </script>
</body>
</html>